<?php
//dragon.hu add @2016.3.2  moneystatistics 

defined('BASEPATH') or die('forbidden');

class Moneystatistics_Controller extends Controller
{
	//private $conn;
	//private $areanum;
	private $areaList;
	private $hostList;
	private $addwayList;
	private $moneytype;
	private $logdb;
	
	function _init()
	{
		check_level(50, segment(0), segment(1));
		set_time_limit(0);
		
		$this->moneytype = array('7' => '竞技币', '85' => '荣耀值', '86' => '宠物晶石',
				'87' => '宠物钥匙', '88' => '刷新令', '90' => '功勋',);
		$this->addwayList = get_type_addway();
		$this->hostList = get_area_list_detal();
		/*$this->areanum = $_SESSION['area_selected_id'];
		if ($this->areanum == '')
		{
			echo "<script>alert('".translate('C_SELECT_AREA_TEXT')."');</script>";
			return;
		}*/
		$this->areaList = get_area_list_option();
		
		//$this->conn = getTmpDbConnect(PLATFORM_ALL, 'all', $this->areanum, DBTYPE_LOG);
		// log数据库
		$this->logdb = $this->load->database('log_db');
	}
	
	function index()
	{
		if(is_submit())
		{
			$startdate = $_POST['row']['starttime'];
			$enddate = $_POST['row']['endtime'];
			$moneytype = $_POST['row']['moneytype'];
			//$serveridtmp = $_POST['row']['area'];
			$serverid = $_POST['areaIds'];
			//$tmp = explode("-", $serveridtmp);
			//$serverid = $tmp[0];
			//var_dump($_POST);
			//print_r($startdate);
			//print_r($enddate);
			//print_r($serverid);
			//exit(0);
			
		}else
		{
			//$startdate = date("Y-m-d");
			//$enddate = date("Y-m-d", strtotime("1 day"));
			$startdate = date("Y-m-d H:i:s", strtotime(date("Y-m-d")));  //默认显示当天
			$enddate = date("Y-m-d H:i:s", time());
			//$serverid = $this->areanum;
			$serverid = array(0);  //默认所有服
			$moneytype = 7;  //默认为竞技币
			//print_r($startdate);
			//print_r($enddate);
			//exit(0);
		}
		//if (empty($this->areanum)){return;}
		//var_dump($sql); 
		//exit(0);
		$datagraph = array();
		if (in_array('0', $serverid))
		{
			foreach ($this->hostList as $k => $v) 
			{
				$sql = "SELECT addway, SUM(add_num) as anum, SUM(cost_num) as cnum FROM money_change WHERE property_type = $moneytype AND serverid = $k AND operation_time BETWEEN '$startdate' AND '$enddate' GROUP BY addway";
				//var_dump ( $sql );
				/*$tmpconn = getTmpDbConnect ( PLATFORM_ALL, 'all', $k, DBTYPE_LOG );
				if (! $tmpconn) 
				{
					continue;
				}
				$tmpconn->query ( $sql );*/
				$this->logdb->query($sql);
				
				while ( $n = /*$tmpconn*/$this->logdb->get_array ( 'default' ) ) 
				{
					$n ['addway'] = $this->addwayList [$n ['addway']] ['chinaname'] . "(" . $n ['addway'] . ")";
					if ($n ['anum'] == 0) // 表示消耗
					{
						$datagraph ['typecost'] [$n ['addway']] += $n ['cnum'];
					} 
					else 
					{
						$datagraph ['typeproduce'] [$n ['addway']] += $n ['anum'];
					}
				}
			}
		}
		else
		{
			for ($i = 0; $i < count($serverid); $i++)
			{
				$sql = "SELECT addway, SUM(add_num) as anum, SUM(cost_num) as cnum FROM money_change WHERE property_type = $moneytype AND serverid = $serverid[$i] AND operation_time BETWEEN '$startdate' AND '$enddate' GROUP BY addway";
				//var_dump($sql);
				/*$tmpconn = getTmpDbConnect(PLATFORM_ALL, 'all', $serverid[$i], DBTYPE_LOG);
				if (!$tmpconn)
				{
					continue;
				}
				$tmpconn->query($sql);*/
				$this->logdb->query($sql);
				
				while($n = /*$tmpconn*/$this->logdb->get_array('default'))
				{
					$n['addway'] = $this->addwayList[$n['addway']]['chinaname']."(".$n['addway'].")";
					if ($n['anum'] == 0)  //表示消耗
					{
						$datagraph['typecost'][$n['addway']] += $n['cnum'];
					}
					else
					{
						$datagraph['typeproduce'][$n['addway']] += $n['anum'];
					}
				}
			}
		}
		
		//var_dump($datagraph);
		//货币总产出，总消耗，产出消耗比，日期
		foreach ($datagraph as $k=>$v)
		{
			if ($k == 'typeproduce')
			{
				foreach ($v as $k1=>$v1)
				{
					$datagraph['otherInfo']['allproduce'] += $v1;
				}
			}
			else
			{
				foreach ($v as $k1=>$v1)
				{
					$datagraph['otherInfo']['allcost'] += $v1;
				}
			}
		}
		if (empty($datagraph['otherInfo']['allproduce']))  $datagraph['otherInfo']['allproduce'] = 0;
		if (empty($datagraph['otherInfo']['allcost']))  $datagraph['otherInfo']['allcost'] = 0;
		$datagraph['otherInfo']['allrate'] = round($datagraph['otherInfo']['allproduce'] / $datagraph['otherInfo']['allcost'], 2);
		$datagraph['otherInfo']['targetdate'] = $startdate.'~'.$enddate;
		//var_dump($datagraph);
		//exit();
		
		set_area_list_view("arealist");
		$this->view->assign('startdate', $startdate);
		$this->view->assign('enddate', $enddate);
		$this->view->assign('areaList', $this->areaList);
		$this->view->assign('moneyType', $this->moneytype);
		$this->view->assign('datagraph', $datagraph);
		
		$this->view->display("gamedata/moneystatistics/index");
	}
	
	
}









?>